/**
  ******************************************************************************
  * @file    main.c
  * @author  测试1组（houtiezhu）
  * @version v0
  * @date    09-07-2019
  *           
  * @verbatim
  **/
/* Includes ------------------------------------------------------------------*/

#include "define.h"
#include "disp.h"
#include "power.h"
#include "clk.h"
#include "gpio.h"
#include "time.h"
#include "uart.h"
#include "spk.h"
#include "flash.h"
#include "adc.h"
#include "algo.h"
#include "bp.h"
#include "func.h"
#include "op.h"
#include "pwm.h"
#include "SD93F115B_tim.h"
#include "SD93F115B_rcc.h"



const uint8  MonthDay2[]	=	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
uint8 	WatchTimeCount;
uint8	fRTC_500ms=0;
uint16  AllTotalTime;	
volatile union byte_bit		OneShotFlag;  

uint8 	TimeCount6_66;

uint8   AlarmN;
uint8   AlarmHour[6];
uint8   AlarmMinute[6];
uint8   AlarmOnOff[6];

uint8    		Second;             
uint8     		Minute;              
uint8     		Hour;               
uint8     		Day;                
uint8     		Month;   
uint16  		AllTotalTime;	
uint16 		TimeInterval;

uint8   	MDay; 


void delay(uint8 i)		//1/160*2=12.5mS
{       
	unsigned char cnt;
	for(cnt=0;cnt<=i;cnt++)
	{
		do{}while(!T160Hz);
		do{}while(T160Hz);
	}
}
void RTC_AlarmInitProgram(void)		//20210830	
{ 	
	//IsRTC1minMode=1;	 //20210902
	RTC_ITConfig(RTC_IT_RTCIE,DISABLE);//关闭半秒中断
	RTC_ClearITStatus(RTC_CR_TAIE);//清闹钟中断标志
	RTC_ITConfig(RTC_IT_TAIE,ENABLE);//使能闹钟中断
	RTC_Cmd(ENABLE);//使能RTC
    RTC_AlarmCmd(ENABLE);
	
}
/*
void InitIrqLevels(void)
{
	;
}
*/

void UserRtc_Init(void)
{

	VIC_InitTypeDef  VIC_InitStructure;
	RTC_InitTypeDef  RTC_InitStructure;
	RTC_AlarmTypeDef  RTC_AlarmStructure;
	RTC_DeInit();
	RCC_APBPeriphClockCmd(RCC_APBPeriph_RTC, ENABLE);//使能RTC时钟
	RCC_APB2PeriphClockConfig(RCC_APBPeriph_RTC,RCC_RTCCLK_XTOSC1,RCC_PWMCLK_DIV1);	//RTC时钟源设置,选择ILRC
	

	
	RTC_StructInit(&RTC_InitStructure);//初始化配置
	RTC_InitStructure.RTC_HourFormat=RTC_HourFormat_24;//选择12小时制
	RTC_InitStructure.RTC_DateStruct.RTC_Year=0x21;//19年
	RTC_InitStructure.RTC_DateStruct.RTC_Month=0x01;//01月
	RTC_InitStructure.RTC_DateStruct.RTC_Date=0x01;//01日
	RTC_InitStructure.RTC_DateStruct.RTC_WeekDay=0x01;//星期1
	RTC_InitStructure.RTC_TimeStruct.RTC_Hours=0x08;//上午(AM)08时
	RTC_InitStructure.RTC_TimeStruct.RTC_Minutes=0x00;//0分
	RTC_InitStructure.RTC_TimeStruct.RTC_Seconds=0x02;//0秒
	
	RTC_StructInit(&RTC_InitStructure);//初始化配置
	
	RTC_AlarmStructure.RTC_AlarmMaskDateOrWeekDay=ENABLE;
	RTC_AlarmStructure.RTC_AlarmMaskHours=ENABLE;
	RTC_AlarmStructure.RTC_AlarmMaskMinutes=ENABLE;
	RTC_AlarmStructure.RTC_AlarmMaskSeconds=DISABLE;
	
	RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours=0x08;//上午(AM)08时
	RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes=0x00;//0分
	RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds=0x01;//01秒
	
	RTC_SetAlarm(RTC_HourFormat_24,&RTC_AlarmStructure);//初始化配置
	
	VIC_InitStructure.VIC_IRQChannel=RTC_IRQn;								//中断向量设置为RTC中断
	VIC_InitStructure.VIC_IRQChannelPriority=VIC_IRQChannelPriority2;//VIC_IRQChannelPriority0;		//中断优先级设置最高
	VIC_InitStructure.VIC_PrioThreshold=VIC_PrioThreshold0;					//中断阈值最小
	VIC_InitStructure.VIC_PrioThresholdCmd=ENABLE;							//使能中断优先级阈值比较
	VIC_InitStructure.VIC_IRQChannelCmd=ENABLE;								//使能对应编号的中断通道
	VIC_InitStructure.VIC_IRQChannelWakeUpLowPowerCmd=ENABLE;				//不使能对应编号的中断可唤醒低功耗模式
	VIC_Init(&VIC_InitStructure);
	
	RTC_AlarmInitProgram();
#if 0	
	RTC_InitTypeDef  RTC_InitStruct;
	RTC_InitStruct.RTC_HourFormat=RTC_HourFormat_24;
	RTC_InitStruct.RTC_TimeStruct.RTC_Hours=0x14;
	RTC_InitStruct.RTC_TimeStruct.RTC_Minutes=0x38;
	RTC_InitStruct.RTC_TimeStruct.RTC_Seconds=0x30;
	
	RTC_InitStruct.RTC_DateStruct.RTC_WeekDay=RTC_Weekday_Thursday;
	RTC_InitStruct.RTC_DateStruct.RTC_Date=0x14;
	RTC_InitStruct.RTC_DateStruct.RTC_Month=RTC_Month_May;
	RTC_InitStruct.RTC_DateStruct.RTC_Year=0x20;
	
	RTC_StructInit(&RTC_InitStruct); 
	
	/*RTC_AlarmTypeDef  RTC_AlarmStruct;
	
	RTC_AlarmStruct.RTC_AlarmMaskDateOrWeekDay=	RTC_AlarmStruct.RTC_AlarmMaskHours= 
	RTC_AlarmStruct.RTC_AlarmMaskMinutes=RTC_AlarmStruct.RTC_AlarmMaskSeconds=DISABLE;
	
	RTC_AlarmStruct.RTC_AlarmDateOrWeekDaySel=RTC_AlarmDateOrWeekDaySel_Date;
	RTC_AlarmStruct.RTC_DateOrWeekDay=0x14;
	RTC_AlarmStruct.RTC_AlarmTime.RTC_Hours=0x14;
	RTC_AlarmStruct.RTC_AlarmTime.RTC_Minutes=0x40;
	RTC_AlarmStruct.RTC_AlarmTime.RTC_Seconds=00;	
	
    RTC_SetAlarm(RTC_HourFormat_24,& RTC_AlarmStruct);
	*/
	VIC_IRQChannelCmd (RTC_IRQn,ENABLE); 

	RTC_ClearITStatus(RTC_IT_RTCIE);
	RTC_ClearITStatus(RTC_IT_TAIE);	
	RTC_ITConfig(RTC_IT_RTCIE, ENABLE);
	RTC_ITConfig(RTC_IT_TAIE, ENABLE);
	
	RTC_AlarmCmd(ENABLE);
    RTC_Cmd(ENABLE);
#endif
}


//#define TIMER0_LOADVAL  64285	//6.7ms
#define TIMER0_LOADVAL  65536-109
//定时器0初始化
void UserTimer0_Init(void)
{
	//RCC_APB1PeriphClockConfig(RCC_APBPeriph_TM0,RCC_TMCLK_IHRC,RCC_TMCLK_DIV128);//RCC_TMCLK_DIV8
	RCC_APB1PeriphClockConfig(RCC_APBPeriph_TM0,RCC_TMCLK_XTOSC1,RCC_TMCLK_DIV2);
	RCC_APBPeriphClockCmd(RCC_APBPeriph_TM0, ENABLE);   //外设时钟使能
	TM_SetAutoReload(TM0,TIMER0_LOADVAL);
	TM_Cmd(TM0, ENABLE);
	//TM_PFDCmd(ENABLE);
	TM_ITCmd(TM0,ENABLE);
	TM_ClearITStatus(TM0);	
}	


void Timer0_int(void)
{
	#if 1
	if (SamplePressure == 1)				//1000/6.66ms*3= 50Hz
		{
			if (SampleSignal == 1)
				{		
					SamplePressure = 0;		 
					SampleSignal = 0; 
					if(RCSensorType)
					{
						//SensorCount2b=ADCDH;
						//SensorCount1b=ADCDM;	
						//SensorCountw = ADC_GetConversionValue()>>8; //16BIT
						if(SDADC_GetITStatus() == SET)
						{
							SDADC_ClearITStatus();
							SensorCountw = ((SDADC_GetConversionValue()&0x00FFFFFF)>>8); //16BIT
						}
					}
					else
					{
						/*
						TC0M&=0x7F;
						SensorCount2b=TC0CH;
						SensorCount1b=TC0CL;	
						do{TC0C=0;}while(TC0C!=0);
						TC0M|=0x80;
						*/
					}
					AutoOffTimew++;					//6.66*3=		20ms
					ADOKFlag=1;
						
						
					if(!KEY_TIME)//长按检测
					{
						if(LongKeyCount[1]<150) LongKeyCount[1]++;
						if(LongKeyCount[1]>=150) LongSetFlag=1;
					}
					else 
					{
						LongKeyCount[1]=0;
						LongSetFlag=0;
					}
					
					if(!KEY_POWER)//长按检测
					{
						if(LongKeyCount[0]<150) LongKeyCount[0]++;
						if(LongKeyCount[0]>=150) 
						{
							LongPowerFlag=1;
						}
						if(LongKeyCount[0]>=50)
						{
							OneKeyFlag=1;
						}
					}
					else 
					{
						LongKeyCount[0]=0;
						LongPowerFlag=0;
						OneKeyFlag=0;
					}
				}
			else{SampleSignal = 1;}
		}
	else{SamplePressure = 1;}	
	
	TimeBase.b++;										//1/6.66ms=150HZ
	WatchTimeCount++;
	if(WatchTimeCount==10)					//6.66ms*10=66.6ms; 66.6*16=1065ms=1s
	{	
		WatchTime.b++;
		WatchTimeCount=0;		
	}
	TimeCount6_66++;
	if(TimeCount6_66>=75)
	{
		fRTC_500ms=1;
		if(TimeCount6_66>=150)		//20210902 6.66*150≈1s
		{
			TimeCount6_66=0;
			AllTotalTime++;
			SecondFlag=1;

			TimeInterval++;
			if(TimeInterval<10) TimeInterval=0;
			fRTC_500ms=0;
		}
	}
	else
	{
		fRTC_500ms=0;
	}
//**************************************************************************************************20110318
	if(T10Hz && GetLeakagePresureFlag)
	{
		GetLeakagePresureFlag=0;
		for(validcount=0;validcount<9;validcount++){LeakageP[validcount]=LeakageP[validcount+1];}
				
		LeakageP[9]=PressureBackup;	
	}
	if(!T10Hz && !GetLeakagePresureFlag){GetLeakagePresureFlag=1;}  	

	if ((KEY_POWER==0)|| (KEY_MEMORYDOWN==0)||(KEY_MEMORYUP==0)|| (KEY_TIME==0)||(KEY_USER==0))  KeyIn = 0;
#endif
}


void Rtc_int(void)
{
#if 1
	fRTC_500ms++;
	if(fRTC_500ms==2)
	{		
			fRTC_500ms = 0;		
			AllTotalTime++;
			Second++;
			SecondFlag=1;
			TimeInterval++;
			if(TimeInterval<10) TimeInterval=0;
			if (Second > 59)			  
			{           
					IsMinute = 1;			  
					Second = 0;			   
					Minute++;
					if (Minute > 59)			
					{	     
							Minute = 0;			   
							Hour++;
							if (Hour > 23) 		  
							{
									Hour = 0;			   
									Day++;			  
									MDay = MonthDay2[Month-1];	   
									if (Month ==2)		   
									{ 			  
											if (YEAR0 == 0 && YEAR1 == 0) MDay = 29;
									}
									if (Day > MDay)		 
									{
											Day = 1; 		  
											Month++;		   
											if (Month > 12)		  
											{
													Month = 1;		  
													Year.b ++;
													if (Year.b > 50) Year.b = 0;
											}
									}
							}
			     	}
			}	          
	}
#endif	
}

void Rtc_1min_interrupt(void)		//20210827
{
#if 1
	IsMinute = 1;			  		   
	Minute++;
	if (Minute > 59)			
	{	     
		Minute = 0;			   
		Hour++;
		if (Hour > 23) 		  
		{
			Hour = 0;			   
			Day++;			  
			MDay = MonthDay2[Month-1];	   
			if (Month ==2)		   
			{ 			  
				if (YEAR0 == 0 && YEAR1 == 0) MDay = 29;
			}
			if (Day > MDay)		 
			{
				Day = 1; 		  
				Month++;		   
				if (Month > 12)		  
				{
					Month = 1;		  
					Year.b ++;
					if (Year.b > 50) Year.b = 0;
				}
			}
		}
	}	
#endif	
}
void AlarmSet(void)
{
	uint8 i;
	for(AlarmN=0;AlarmN<2;AlarmN++)
	{
		for(i=0;i<3;i++)
		{
			DarkAllIcon();
			showalarm();
			while(!KEY_TIME);
			keydelay();
			AutoOffTimew=0;			
			while(KEY_TIME)
			{
				delay(1);
				flashalarm(i);
				if((AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag)||!KEY_POWER){IsPowerOn=0; goto AlarmEnd;}
				
				if(!KEY_MEMORYUP)
				{
					AutoOffTimew=0;
				KEYUP:
					if(i==0)
					{
						AlarmHour[AlarmN]++;
						if(AlarmHour[AlarmN]>23) AlarmHour[AlarmN]=0;
					}
					else if(i==1)
					{
						AlarmMinute[AlarmN]++;
						if(AlarmMinute[AlarmN]>59) AlarmMinute[AlarmN]=0;
					}
					else if(i==2)
					{
						AlarmOnOff[AlarmN]++;
						if(AlarmOnOff[AlarmN]>1) AlarmOnOff[AlarmN]=0;
					}
					showalarm();
					delay(8);
					while(!KEY_MEMORYUP)
					{
						if(AutoOffTimew>TWO) goto KEYUP;
					}
					keydelay();
				}
				if(!KEY_MEMORYDOWN)
				{
					AutoOffTimew=0;
				KEYDOWN:
					if(i==0)
					{
						AlarmHour[AlarmN]--;
						if(AlarmHour[AlarmN]>23) AlarmHour[AlarmN]=23;
					}
					else if(i==1)
					{
						AlarmMinute[AlarmN]--;
						if(AlarmMinute[AlarmN]>59) AlarmMinute[AlarmN]=59;
					}
					else if(i==2)
					{
						AlarmOnOff[AlarmN]++;
						if(AlarmOnOff[AlarmN]>1) AlarmOnOff[AlarmN]=0;
					}
					showalarm();
					delay(8);
					while(!KEY_MEMORYDOWN)
					{
						if(AutoOffTimew>TWO) goto KEYDOWN;
					}
					keydelay();
				}
			}
		}
	}
AlarmEnd:
	nop4();
}






